package www.dianbaozu.com.ui.server

import androidx.hilt.lifecycle.ViewModelInject
import www.wen.com.common.lib.base.module.BaseAbsViewModel
import www.wen.com.common.lib.data.entities.Result
import www.wen.com.common.lib.data.entities.ServerSelfListEntity
import www.wen.com.common.lib.data.repository.AppDataManager


/**
 * @author WEN
 * @Description:创建新的服务器
 * @date 2020//25 17:48
 */
class ServerViewModel @ViewModelInject constructor(appDataManager: AppDataManager) :
        BaseAbsViewModel<ServerSelfListEntity, ServerSelfListEntity.Content>(appDataManager) {

    /**
     * 手动添加数据不需要再添加进集合
     */
    val mMap:Map<String,ServerSelfListEntity> = HashMap()

    /**
     * 手动添加的预览服务器数据不需要再从服务器读取
     */
    val mPreviewServerMap = HashMap<String,String>()

    override val mLimit = 150

    override suspend fun requestListData(requiredParameter:Any): Result<ServerSelfListEntity>{
        val result = appDataManager.requestServerSelfList(mPage,mLimit)
        if(result is Result.Success){
            result.data.content.find {
                it.serverId == mPreviewServerMap[PREVIEW_KEY]
            }.apply {
                result.data.content.toMutableList().remove(this)
            }
        }
        return result
    }

    override fun dataConversion(data: ServerSelfListEntity): List<ServerSelfListEntity.Content> {
        if(mIsFirstLoading && data.content.isNotEmpty()) data.content[0].isSelected = true

        if(data.content.isNotEmpty() && mIsRefresh) data.content[0].isSelected = true
        return  data.content
    }

    companion object{
        const val PREVIEW_KEY = "PREVIEW_KEY"
    }


}


